﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using StringUtilHelp;

namespace WinFormUtilHelpV2Plus
{
    public static class WinFormToolV2Plus
    {
        /// <summary>
        /// 检查控件Text是否为空
        /// </summary>
        /// <typeparam name="T">需要检查的控件</typeparam>
        /// <param name="t">需要检查的控件</param>
        /// <param name="txt">当不为空的时候，返回控件Text值</param>
        /// <returns>若为空则TRUE,反之则FALSE</returns>
        public static bool CheckCtrlTextEmpty<T>(this T t, out string txt) where T : Control
        {
            try
            {
                txt = t.Text.Trim();
                txt = StringToolV2.ClearBlank(t.Text.Trim());
                return txt.Length == 0;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("CheckCtrlTextEmpty Exception:{0}", ex.Message.Trim()));
            }

        }
        public static bool CheckCtrlTextEmpty<T>(this T t, out string txt, string emptyErrorMsg) where T : Control
        {
            try
            {
                bool _checkResult = CheckCtrlTextEmpty<T>(t, out txt);
                if (_checkResult && !string.IsNullOrEmpty(emptyErrorMsg))
                    MessageBox.Show(emptyErrorMsg, "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return _checkResult;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("CheckCtrlTextEmpty Exception:{0}", ex.Message.Trim()));
            }
        }
        public static bool CheckCtrlTextPointLen<T>(this T t, out string txt, int pointLen, string emptyErrorMsg, string pointLenError) where T : Control
        {
            try
            {
                bool _checkReuslt = !CheckCtrlTextEmpty<T>(t, out txt, emptyErrorMsg);
                if (_checkReuslt && !string.IsNullOrEmpty(emptyErrorMsg))
                {
                    _checkReuslt = txt.Length == pointLen;
                    if (!_checkReuslt && !string.IsNullOrEmpty(pointLenError))
                    {
                        MessageBox.Show(pointLenError, "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                return _checkReuslt;
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("CheckCtrlTextPointLen Exception:{0}", ex.Message.Trim()));
            }
        }
    }
}
